package com.yimi.yinhepda.views.dispatch_module;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import butterknife.Bind;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.bean.DispatchBackScanChildEntity;
import com.yimi.yinhepda.entity.bean.DispatchBackScanEntity;
import com.pda.ym.utils.SoundVibratorManager;
import com.pda.ym.utils.StringUtils;
import com.yimi.yinhepda.base.BaseInitActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 配送退单撤销界面
 *
 * @author 000432
 * @date 2018/3/8 0008
 */
public class DispatchBackScanDelActivity extends BaseInitActivity {

    @Bind(R.id.back_scan_del_input)
    EditText mDelInput;
    @Bind(R.id.back_scan_del_btn)
    Button mDelBtn;
    @Bind(R.id.back_scan_del_back)
    Button mDelBackBtn;
    @Bind(R.id.back_scan_del_ok)
    Button mDelOkBtn;
    @Bind(R.id.back_scan_del_listview)
    ListView mListView;
    /**
     * 撤销统计
     */
    @Bind(R.id.manaul_del_count)
    TextView mDelCount;
    private List<DispatchBackScanEntity> mScanList;
    private DeleteScanedAdapter mAdapter;
    private ArrayList<DeleteEntity> deleteEntityLists = new ArrayList<>();

    public DispatchBackScanDelActivity() {
        super(true);
    }

    @Override
    public String getPageName() {
        return "手动删除单号";
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_del_dispatch_back_scan;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initEvent() {
        mDelBtn.setOnClickListener(this);
        mDelBackBtn.setOnClickListener(this);
        mDelOkBtn.setOnClickListener(this);
    }

    @Override
    public void initData() {
        ((TextView) findViewById(R.id.title)).setText("手动删除单号");
        findViewById(R.id.num).setVisibility(View.GONE);

        mScanList = DispatchBackScanActivity.mScanList;

        mAdapter = new DeleteScanedAdapter(this, deleteEntityLists);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scan_del_btn:
                doScan(mDelInput.getText().toString().trim());
                break;
            case R.id.back_scan_del_back:
            case R.id.back_scan_del_ok:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void handleBarCode(String result) {
        if (result == null) {
            SoundVibratorManager.playSound(2);
            showToast("运单号扫描失败");
        } else {
            mDelInput.setText(result);
            doScan(result);
        }
    }

    /**
     * 处理扫描
     */
    protected void doScan(String result) {
        //判断是否是主单号
        boolean isMasterWaybillNo = StringUtils.isMasterWayBillNo(result);

        if (!StringUtils.isWaybillNo(result) && !StringUtils.isPackgeNo(result) && !isMasterWaybillNo) {
            SoundVibratorManager.playSound(2);
            showToast("单号或包号不规范!");
            return;
        }

        if (isMasterWaybillNo) {
            //撤销主单号
            Iterator<DispatchBackScanEntity> scanEntityIterator = mScanList.iterator();
            while (scanEntityIterator.hasNext()) {
                DispatchBackScanEntity scanEntity = scanEntityIterator.next();
                if (scanEntity.getWayBillNo().equals(result)) {
                    boolean isExist = false;
                    for (DeleteEntity item : deleteEntityLists) {
                        if (item.getWaybill().equals(scanEntity.getWayBillNo())) {
                            item.setScanedwaybillsNum("0");
                            item.setCancelwaybillsNum((Integer.parseInt(item.getCancelwaybillsNum()) + scanEntity.getChildEntityList().size()) + "");
                            deleteEntityLists.remove(item);
                            deleteEntityLists.add(0, item);
                            isExist = true;
                            break;
                        }
                    }
                    if (!isExist) {
                        DeleteEntity mDeleteEntity = new DeleteEntity();
                        mDeleteEntity.setWaybill(scanEntity.getWayBillNo());
                        mDeleteEntity.setScanedwaybillsNum("0");
                        mDeleteEntity.setCancelwaybillsNum(scanEntity.getChildEntityList().size() + "");
                        deleteEntityLists.add(mDeleteEntity);
                    }
                    scanEntityIterator.remove();
                    break;
                }
            }
        } else {
            //撤销子单号
            //遍历子单号是否在列表中
            boolean inChildList = false;
            Iterator<DispatchBackScanEntity> scanEntityIterator = mScanList.iterator();
            DispatchBackScanEntity scanEntity = null;
            int childCount = 0;
            while (scanEntityIterator.hasNext()) {
                scanEntity = scanEntityIterator.next();
                childCount = scanEntity.getChildEntityList().size();
                Iterator<DispatchBackScanChildEntity> childEntityIterator = scanEntity.getChildEntityList().iterator();
                boolean isBreak = false;
                while (childEntityIterator.hasNext()) {
                    DispatchBackScanChildEntity childEntity = childEntityIterator.next();
                    if (result.equals(childEntity.getChildWaybillNo())) {
                        inChildList = true;
                        childEntityIterator.remove();
                        isBreak = true;
                        break;
                    }
                }
                if (scanEntity.getChildEntityList().size() <= 0) {
                    //子单为空是删除主单
                    scanEntityIterator.remove();
                }
                if (isBreak) {
                    break;
                }
            }
            if (!inChildList) {
                SoundVibratorManager.playSound(2);
                showToast("运单号未扫描或已删除！");
                return;
            }

            boolean isExist = false;
            for (DeleteEntity item : deleteEntityLists) {
                if (item.getWaybill().equals(scanEntity.getWayBillNo())) {
                    item.setScanedwaybillsNum((Integer.parseInt(item.getScanedwaybillsNum().toString()) - 1) + "");
                    item.setCancelwaybillsNum((Integer.parseInt(item.getCancelwaybillsNum().toString()) + 1) + "");
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                DeleteEntity mDeleteEntity = new DeleteEntity();
                mDeleteEntity.setWaybill(scanEntity.getWayBillNo());
                mDeleteEntity.setScanedwaybillsNum((scanEntity.getChildEntityList().size() > 0 ? (childCount - 1) : 0) + "");
                mDeleteEntity.setCancelwaybillsNum("1");
                deleteEntityLists.add(mDeleteEntity);
            }
        }

        //更新显示信息
        mAdapter.notifyDataSetChanged();
        SoundVibratorManager.playSound(1);

        //计算撤销的票件
        excCount();
    }

    /**
     * 计算撤销的票件
     */
    private void excCount() {
        int count = 0;
        for (DeleteEntity item : deleteEntityLists) {
            count += Integer.parseInt(item.getCancelwaybillsNum());
        }
        mDelCount.setText(deleteEntityLists.size() + "/" + count);
    }

    static class DeleteScanedAdapter extends BaseAdapter {
        private Context context;
        private List<DeleteEntity> dataList;

        public DeleteScanedAdapter(Context context, List<DeleteEntity> dataList) {
            this.context = context;
            this.dataList = dataList;
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            DeleteWayBillHolder mHolder;
            if (convertView == null) {
                mHolder = new DeleteWayBillHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.activity_del_scaned_waybillitem, null);
                mHolder.waybillno = (TextView) convertView.findViewById(R.id.cancelwaybill);
                mHolder.scanedwaybillNum = (TextView) convertView.findViewById(R.id.scanedwaybillnum);
                mHolder.cancelwayBillNum = (TextView) convertView.findViewById(R.id.canceledwaybillnum);

                convertView.setTag(mHolder);
            } else {
                mHolder = (DeleteWayBillHolder) convertView.getTag();
            }
            mHolder.waybillno.setText(dataList.get(position).getWaybill());
            mHolder.scanedwaybillNum.setText(dataList.get(position).getScanedwaybillsNum());
            mHolder.cancelwayBillNum.setText(dataList.get(position).getCancelwaybillsNum());

            return convertView;
        }
    }

    static class DeleteWayBillHolder {
        TextView waybillno;
        TextView scanedwaybillNum;
        TextView cancelwayBillNum;
    }

    static class DeleteEntity {
        private String waybill;// 运单号
        private String scanedwaybillsNum;// 已扫数量
        private String cancelwaybillsNum;// 撤销数量

        public String getWaybill() {
            return waybill;
        }

        public void setWaybill(String waybill) {
            this.waybill = waybill;
        }

        public String getScanedwaybillsNum() {
            return scanedwaybillsNum;
        }

        public void setScanedwaybillsNum(String scanedwaybillsNum) {
            this.scanedwaybillsNum = scanedwaybillsNum;
        }

        public String getCancelwaybillsNum() {
            return cancelwaybillsNum;
        }

        public void setCancelwaybillsNum(String cancelwaybillsNum) {
            this.cancelwaybillsNum = cancelwaybillsNum;
        }
    }
}
